﻿using System;
using System.Windows.Forms;
using System.Threading;
using Lottery.App.Model;
using Lottery.App.BLL;

namespace UI
{
    public partial class Dj : Form
    {
        /// <summary>
        /// 期号
        /// </summary>
        public string QH
        {
            set
            {
                txt_qh.Text = value;
            }
            get
            {
                return txt_qh.Text;
            }
        }
        /// <summary>
        /// 已选的开奖号码
        /// </summary>
        public string Result { get; set; }
        public Dj()
        {
            InitializeComponent();
            showNumber1.NumberChanged += new UI.Control.ShowNumber.NumberChangedHandler(showNumber1_NumberChanged);
        }

        void showNumber1_NumberChanged()
        {
            foreach (System.Windows.Forms.Control c in p_btns.Controls)
            {
                Control.Button b = c as Control.Button;
                b.Enabled = true;
            }
            lb_info.Text = string.Format(lb_info.Tag.ToString(), showNumber1.NumberCount, showNumber1.CanSelectedCount - showNumber1.NumberCount);
            if (showNumber1.NumberCount == showNumber1.CanSelectedCount)
            {
                btn_OK.Enabled = true;
                foreach (System.Windows.Forms.Control c in p_btns.Controls)
                {
                    Control.Button b = c as Control.Button;
                    b.Enabled = b.Checked;
                }
            }
            else
            {
                btn_OK.Enabled = false;
            }
        }

        private void dj_Load(object sender, EventArgs e)
        {
            this.QH = Lottery.App.GlobalData.NextQH;
            lb_info.Text = string.Format(lb_info.Tag.ToString(), showNumber1.NumberCount, showNumber1.CanSelectedCount - showNumber1.NumberCount);
        }

        private void button1_CheckedChange(UI.Control.Button sender, EventArgs e)
        {
            if (!sender.Checked)
            {
                showNumber1.RemoveNumber(sender.Value);
            }
            else
            {
                if (!showNumber1.AddNumber(sender.Value))
                {
                    sender.Checked = false;
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (this.QH.Trim().Length != 11)
            {
                MessageBox.Show("期号错误!", "提示");
                return;
            }

            RecordInfo r = new LotteryManager().Get(this.QH.Trim());
            if (r != null)
            {
                MessageBox.Show("期号已存在!不可重复添加!", "提示");
                return;
            }


            btn_OK.Enabled = false;
            btn_exit.Enabled = false;
            btn_clear.Enabled = false;
            LoadingManager.Show();

            string[] strs = new string[] { 
                this.QH,
                showNumber1.Value.Trim()
            };

            Thread t = new Thread(InsertData);
            t.IsBackground = true;
            t.Start(strs);
        }

        delegate void InsertEndHandler();

        void InsertData(object o)
        {
            string[] strs = o as string[];
            if (strs != null)
            {
                new LotteryManager().Save(strs[0], strs[1]);

                Invoke(new InsertEndHandler(InsertEnd));
            }
            else
            {
                MessageBox.Show("出错了.", "提示");
            }
        }

        void InsertEnd()
        {
            showNumber1.ClearNumber();
            foreach (System.Windows.Forms.Control c in p_btns.Controls)
            {
                Control.Button b = c as Control.Button;
                b.Enabled = true;
                b.Checked = false;
            }

            LoadingManager.Hide();
            //btn_OK.Enabled = true;
            btn_exit.Enabled = true;
            btn_clear.Enabled = true;
            this.QH = Lottery.App.GlobalData.NextQH;
            
            MessageBox.Show("添加成功!", "提示");
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            ClearNumber();
        }

        private void ClearNumber()
        {
            showNumber1.ClearNumber();
            foreach (System.Windows.Forms.Control c in p_btns.Controls)
            {
                Control.Button b = c as Control.Button;
                b.Enabled = true;
                b.Checked = false;
            }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
